/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EscolhaPK.java
 *
 * Created on 09/09/2010, 21:10:16
 */
package geraphp;

import br.gov.ce.sejus.util.tabela.Tabela;
import java.util.ArrayList;
import org.jdesktop.application.Action;

/**
 *
 * @author alysonsmelo
 */
public class EscolhaPK extends javax.swing.JDialog {

    public static String nomeTabela;
    public static ConexaoBD conexao;
    public static String nomeAtributo;

    /** Creates new form EscolhaPK */
    public EscolhaPK(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setTitle("Escolher uma PK.");
        this.setResizable(false);
        this.setModal(true);

        popularTabelas();
    }

    public void popularTabelas() {
        Tabela tabela = new Tabela();
        ArrayList listaCampos = conexao.obterCampos(nomeTabela);

        String[] colunas = {"PK", "Nome", "Tipo", "Tamanho", "É nulo"};

        tblAtributos = tabela.criarTabela(tblAtributos, colunas, listaCampos);
        lblNomeTabela.setText(nomeTabela);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblAtributos = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        lblCampoPk = new javax.swing.JLabel();
        lblNomeTabela = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(geraphp.GeraPHPApp.class).getContext().getResourceMap(EscolhaPK.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, -1));

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, -1, -1));

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tblAtributos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblAtributos.setName("tblAtributos"); // NOI18N
        tblAtributos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblAtributos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblAtributosMouseClicked(evt);
            }
        });
        tblAtributos.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tblAtributosKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(tblAtributos);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 60, 541, 110));

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(geraphp.GeraPHPApp.class).getContext().getActionMap(EscolhaPK.class, this);
        jButton1.setAction(actionMap.get("escolherCampoPK")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 190, -1, -1));

        jButton2.setAction(actionMap.get("fechar")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 190, -1, -1));

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 180, -1, -1));

        lblCampoPk.setText(resourceMap.getString("lblCampoPk.text")); // NOI18N
        lblCampoPk.setName("lblCampoPk"); // NOI18N
        getContentPane().add(lblCampoPk, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 180, -1, -1));

        lblNomeTabela.setText(resourceMap.getString("lblNomeTabela.text")); // NOI18N
        lblNomeTabela.setName("lblNomeTabela"); // NOI18N
        getContentPane().add(lblNomeTabela, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 10, -1, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
    }

    @Action
    public void fechar() {
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void tblAtributosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblAtributosMouseClicked
        if (tblAtributos.getSelectedRowCount() > 0) {
            lblCampoPk.setText(tblAtributos.getModel().getValueAt(tblAtributos.getSelectedRow(), 1) + "");

        }
    }//GEN-LAST:event_tblAtributosMouseClicked

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        popularTabelas();
    }//GEN-LAST:event_formWindowOpened

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        popularTabelas();
        lblCampoPk.setText("");
    }//GEN-LAST:event_formWindowActivated

    private void tblAtributosKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tblAtributosKeyPressed
        if (tblAtributos.getSelectedRowCount() > 0) {
            lblCampoPk.setText(tblAtributos.getModel().getValueAt(tblAtributos.getSelectedRow(), 1) + "");

        }
    }//GEN-LAST:event_tblAtributosKeyPressed

    @Action
    public void escolherCampoPK() {
        nomeAtributo = lblCampoPk.getText();
        dispose();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCampoPk;
    private javax.swing.JLabel lblNomeTabela;
    private javax.swing.JTable tblAtributos;
    // End of variables declaration//GEN-END:variables
}
